﻿$(function () {
    $("#departmentCode").ligerComboBox({
        width: 120,
        cancelable: false,
        selectBoxWidth: 200,
        selectBoxHeight: 200,
        valueField: 'DepartmentCode',
        textField: 'DepartmentName',
        valueFieldID: 'hdnDepartmentCode',
        value:-1,
        treeLeafOnly: false,
        tree: {
            url: '/Sysbase/Department/GetAllDepartment?ram' + Math.random(),
            parms: { isAll: 1},
            ajaxType: 'get',
            idFieldName: "DepartmentCode",
            parentIDFieldName: "ParentId",
            textFieldName: "DepartmentName",
            single: true,
            onSuccess: function (data) {
                $(".l-tree").css('width', '200px');
            }
        }
    });

    $('#btnSearch').click(function () {
        search();
    });

    $('#btnAdd').click(function () {
        edit();
    });

    $('#btnSearch').click();
});

//加载列表
function initTable(queryData, pageNumber) {
    $("#maingrid").ligerGrid({
        height: '100%',
        url: '/Sysbase/User/Search?ram=' + Math.random(),
        parms: queryData,
        page: pageNumber,
        pageSize: 30,
        rownumbers: true,
        heightDiff: -5,
        columns: [
            { display: '工号', name: 'UserId', minWidth: 80 },
            { display: '姓名', name: 'UserName', minWidth: 80 },
            { display: '性别', name: 'Sex', width: 60 },
            { display: '出生年月', name: 'Birthday', minWidth: 100 },
            { display: '电话', name: 'Telephone', minWidth: 100 },
            { display: '角色', name: 'RoleName', minWidth: 100 },
            {
                display: '状态', name: 'Status', width: 80, render: function (row) {
                    var html = "";
                    if (row.Status == 0) {
                        html = "正常";
                    }
                    else if (row.Status == 1) {
                        html = "停用";
                    }
                    else {
                        html = "其他";
                    }
                    return html;
                }
            },
            { display: '所属部门', name: 'DepartmentName', minWidth: 100 },
            { display: 'IC卡号', name: 'ICCode', minWidth: 100 },
            { display: '条码号', name: 'BarCode', minWidth: 100 },
            { display: '职称', name: 'PositionTitle', minWidth: 100 },
            { display: '职务', name: 'JobTitle', minWidth: 100 },
            {
                display: '操作', name: '', minWidth: 100, render: function (row) {
                    return '<div style="heigth:100%;width:80px;text-align: left;margin:0px auto;"><a href="javascript:void(0);" class="a-edit" onclick="edit(' + row.Id + ')">编辑</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:void(0);" class="a-del" onclick="del(' + row.Id + ')">删除</a></div>';
                }
            }
        ]
    });
    GridNoData($("#maingrid"));
}

//搜索
function search(pageNumber) {
    var queryData = {
        userId: $("#txtUserId").val(),
        name: $("#txtName").val(),
        departmentCode: $("#hdnDepartmentCode").val()
    }
    initTable(queryData, pageNumber);
}

//添加/编辑
function edit(id) {
    $.ligerDialog.open({
        height: 530,
        width: 520,
        title: '编辑用户',
        url: '/Sysbase/User/Edit?id=' + id,
        showMax: false,
        showToggle: false,
        showMin: false,
        isResize: false,
        slide: false,
        data: {}
    });
}

//删除
function del(id) {
    $.ligerDialog.confirm('确定删除？', function (value) {
        if (value) {
            var url = "/Sysbase/User/Delete";
            $.ajax({
                url: url,
                type: "POST",
                cache: false,
                data: { id: id },
                success: function (data) {
                    var d = $.parseJSON(data);
                    if (d.Result == 1) {
                        $.ligerDialog.success("删除成功");
                        $('#btnSearch').click();
                    }
                    else if (d.Result == 0) {
                        $.ligerDialog.error(d.Body);
                    }
                    else {
                        $.ligerDialog.error("程序错误");
                    }
                },
                error: function () {
                    $.ligerDialog.error("程序错误");
                }
            });
        }
    });
}
